package com.example.treehole.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.treehole.bean.User;
import com.example.treehole.common.BaseResult;
import com.example.treehole.common.PasswordEncryptor;
import com.example.treehole.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;


    @GetMapping("/login")
    public ResponseEntity<?> login(String phone, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone).eq(User::getPassword, PasswordEncryptor.encrypt(password));
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            return BaseResult.error("用户名或密码错误");
        } else {
            one.love = new ArrayList<>();
            return BaseResult.ok("登录成功", one);
        }
    }

    @GetMapping("/getUserById")
    public ResponseEntity<?> getUserById(String userid) {
        User one = userService.getById(userid);
        return BaseResult.ok("", one);
    }

    @GetMapping("/get")
    public ResponseEntity<?> get() {
        return BaseResult.ok("获取成功", userService.lambdaQuery().ne(User::getId, "admin").list());
    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(String id) {
        userService.removeById(id);
        return BaseResult.ok("操作成功");
    }


    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody User user) {
		if (user.getPhone().length()!=11) {
                return BaseResult.error("手机号位数不正确");
		}
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone());
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            return BaseResult.error("手机号已存在");
        }
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, user.getEmail());
        one = userService.getOne(queryWrapper);
        if (one != null) {
            return BaseResult.error("邮箱已存在");
        }
        user.setPassword(PasswordEncryptor.encrypt(user.password));
        boolean save = userService.save(user);
        if (save) {
            return BaseResult.ok("注册成功");
        } else {
            return BaseResult.error("注册失败");
        }

    }

    @PostMapping("/update")
    public ResponseEntity<?> update(@RequestBody User user) {
        boolean save = userService.updateById(user);
        if (save) {
            return BaseResult.ok("更新成功");
        } else {
            return BaseResult.error("更新失败");
        }
    }

    @PostMapping("/updatePassword")
    public ResponseEntity<?> updatePassword(@RequestBody User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, user.getEmail()).eq(User::getPhone, user.phone);
        User one = userService.getOne(queryWrapper);
        if (one != null){
            if(user.getPassword().length()>6){
                return BaseResult.ok("位数超过6位");
            }
            one.setPassword(PasswordEncryptor.encrypt(user.password));
            userService.updateById(one);
            return BaseResult.ok("修改成功");
        } else {
            return BaseResult.ok("邮箱不正确");
        }
    }

}
